package com.yckj.ytracker.api.util;


import com.yckj.ytracker.api.collect.LogSpanCollector;
import com.yckj.ytracker.api.collect.SpanCollector;
import com.yckj.ytracker.api.model.Span;
import com.yckj.ytracker.api.trace.ITracerServer;
import com.yckj.ytracker.api.trace.TraceContext;
import com.yckj.ytracker.api.trace.TracerServer;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 调用链使用辅助类
 */
public class TrackerHelper {
    private static TraceContext traceContext;
    private static SpanCollector collector;
    private static volatile AtomicBoolean init = new AtomicBoolean(false);

    /**
     * 初始化上下文和收集器
     */
    public static void init() {
        if (!init.compareAndSet(false, true)) {
            return;
        }
        traceContext = new TraceContext();
        collector = new LogSpanCollector();
    }

    /**
     * 获取调用链上下文
     *
     * @return 上下文
     */
    public static TraceContext getTraceContext() {
        return traceContext;
    }

    /**
     * 获得当前节点
     *
     * @return 当前节点
     */
    public static Span getCurrentSpan() {
        return traceContext.getCurrentSpan();
    }

    /**
     * 获得调用链接口
     *
     * @return 调用链接口
     */
    public static ITracerServer getTracer() {
        return new TracerServer(traceContext, collector);
    }

    /**
     * 获得数据收集器
     *
     * @return 数据收集器
     */
    public static SpanCollector getSpanCollector() {
        return collector;
    }
}
